package br.com.sgc.dao;

import java.util.List;

import javax.ejb.Stateless;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import br.com.sgc.entities.produto.Fornecedor;
import br.com.sgc.enuns.TipoOrdenacao;

@Stateless
public class FornecedorDao extends GenericDao<Fornecedor>{

	private static final long serialVersionUID = 9049394917719368425L;

	public Fornecedor findByCnpj(String cnpj, Long clinicaId) {
		Criteria criteria = getCriteria(Fornecedor.class);
		criteria.add(Restrictions.eq("cnpj", cnpj));
		criteria.add(Restrictions.eq("clinica.id", clinicaId));
		Fornecedor fornecedor = (Fornecedor) criteria.uniqueResult();
		if(fornecedor != null){
			if(fornecedor.getEnderecos() != null)
			fornecedor.getEnderecos().size();
			
			if(fornecedor.getTelefones() != null)
			fornecedor.getTelefones().size();
		}
		return fornecedor;
	}
	
	
	public Long count(Fornecedor modelClass, Long clinicaId) {
		Long size = 0L;
		Criteria criteria = getCriteria(Fornecedor.class);
		createCriteriaRestrictions(modelClass, clinicaId, criteria);
		criteria.setProjection(Projections.rowCount());
		size = (Long) criteria.uniqueResult();
        return size;
	}
	
	public List<Fornecedor> findAll(Fornecedor modelClass, Long clinicaId, Integer startPage, Integer maxPage, String orderField, Boolean initializeDependecies) {
		return findAll(modelClass, clinicaId, startPage, maxPage, orderField, initializeDependecies, TipoOrdenacao.ASC);
	}
	
	@SuppressWarnings("unchecked")
	public List<Fornecedor> findAll(Fornecedor modelClass, Long clinicaId, Integer startPage, Integer maxPage, String orderField, Boolean initializeDependecies, TipoOrdenacao tipoOrdenacao){
		
		Criteria criteria = getCriteria(Fornecedor.class);
		criteria.setFirstResult(startPage);
		criteria.setMaxResults(maxPage);
		
		createCriteriaRestrictions(modelClass, clinicaId, criteria);
		
		if(orderField != null && !orderField.trim().equals("")) {
			if(tipoOrdenacao == TipoOrdenacao.ASC){
				criteria.addOrder(Order.asc(orderField));
			} else {
				criteria.addOrder(Order.desc(orderField));
			}
			
		}
		
		List<Fornecedor> lista = criteria.list();
		if(initializeDependecies) {
			for(Fornecedor t : lista){
				if(t.getEnderecos() != null)
					t.getEnderecos().size();
					
				if(t.getTelefones() != null)
					t.getTelefones().size();
			}
		}
			
		return lista;
	}

	private void createCriteriaRestrictions(Fornecedor modelClass, Long clinicaId, Criteria criteria) {
		if(modelClass != null){
			if(modelClass.getClinica() != null && modelClass.getClinica().getId() != null){
				criteria.add(Restrictions.eq("clinica.id", modelClass.getClinica().getId()));
			}
			if(modelClass.getRazaoSocial() != null && !modelClass.getRazaoSocial().isEmpty())
				criteria.add(Restrictions.ilike("razaoSocial", modelClass.getRazaoSocial(), MatchMode.ANYWHERE));
			
			if(modelClass.getCnpj() != null && !modelClass.getCnpj().isEmpty())
				criteria.add(Restrictions.eq("cnpj", modelClass.getCnpj()));
			
		}
	}
	
	

}
